<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
    String contextPath = (String) request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link type="text/css" href="<%=contextPath %>/css/reset.css"
	rel="stylesheet" />
<link rel="stylesheet" href="<%=contextPath %>/css/edit.css" type="text/css" />
<link rel="stylesheet" type="text/css"
	href="<%=contextPath %>/js/jquery-easyui-1.3/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=contextPath %>/css/rs.css">
<link rel="stylesheet" type="text/css"
	href="<%=contextPath %>/js/jquery-easyui-1.3/themes/icon.css">
<link href="<%=contextPath %>/js/uploadify/uploadify.css" rel="stylesheet" type="text/css" />	
<script type="text/javascript"
	src="<%=contextPath %>/js/jquery-easyui-1.3/jquery-1.7.2.min.js"></script>
<script type="text/javascript"
	src="<%=contextPath %>/js/jquery-easyui-1.3/jquery.easyui.min.js"></script>
<script type="text/javascript"
	src="<%=contextPath %>/js/jquery-easyui-1.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript" src="<%=contextPath %>/js/uploadify/jquery.uploadify.min.js"></script>
<script>
	var selectedIds = "";
	var status = "";
	$(document).ready(function(){
		$('#wndAdd').window({closed:true});//  data-options="closed:true"
		$('#wndSearch').window({closed:true});
		$('#wndDetail').window({closed:true});
	});
	$(function() {
		$('#dataTable').datagrid({			
			title : '排班管理',
			height : 500,
			nowrap : true,
			autoRowHeight : false,
			striped : true,
			collapsible : false,
			url : '<%=contextPath%>/admin/scheduleAction.do?method=doctorScheduleList',
			sortName : 'id',
			sortOrder : 'desc',
			remoteSort : false,
			idField : 'id',
			singleSelect :true,
			checkOnSelect:false,
			selectOnCheck:true,
			pageList: [10,30,50],
			onCheck:function(rowIndex,rowData){
				selectedIds = rowData.id+",";
				status = rowData.status;
			},
			/*onSelectAll:function(rows){
				for(var i=0;i<rows.length;i++){
					selectedIds+=rows[i].id+",";
				}
			},
			 onUncheckAll:function(rows){
				selectedIds='';
			}, */
			columns : [ [ {
				field : 'ck',
				checkbox : true,
				hidden : false
			}, {
				field : 'id',
				title : 'ID',
				width : 30,
				sortable : true
			}, {
				field : 'hos_name',
				title : '所属医院',
				width : 150,
				sortable : true
			}, {
				field : 'sec_name',
				title : '科&nbsp;&nbsp;&nbsp;&nbsp;室',
				width : 80,
				sortable : true
			}, {
				field : 'doc_name',
				title : '姓名',
				width : 100,
				sortable : true
			}, {
				field : 'gender',
				title : '性别',
				width : 50,
				formatter:function(value,row,index){
					if(value=='m') return "男";
					if(value=='f') return "女";
					return '';
				}
			}, {
				field : 'pro_title',
				title : '专业职称',
				width : 80,
				formatter:function(value,row,index){
					return row.pro_title_cn;
				}
			},{
				field : 'sc_date',
				title : '排班日期',
				width : 100,
				sortable : true
			}, {
				field : 'batch',
				title : '时段',
				width : 50,
				formatter:function(value,row,index){
					if(value=='1') return "上午";
					if(value=='2') return "下午";
					return '';
				}
			},
			{
				field : 'status',
				title : '排班状态',
				width : 100,
				formatter:function(value,row,index){
					if(value=='0') return "有效";
					if(value=='1') return "过期";
					return '';
				}
			} , 
			
			{
				field : 'opt',
				title : '操作',
				width : 100,
				align : 'center',
				rowspan : 2,
				formatter : function(value, row,index) {
					return '<a style="color:red" href="#" mce_href="#" onClick="editSingle('+row.id+')">编辑</a>&nbsp;&nbsp;<a style="color:red" href="#" mce_href="#" onClick="deleteSingle('+row.id+')">删除</a>&nbsp;&nbsp;<a style="color:red" href="#" mce_href="#" onClick="cancleSingle('+row.id+')">取消排班</a>';
				},
				hidden :true
			} ] ],

			pagination : true,
			rownumbers : true,
			
			toolbar : [ {
				id : 'btnadd',
				text : '新增',
				iconCls : 'icon-add',
				handler : function() {
					$("#addForm").form('clear');
					$('#wndAdd').window({
						closed:false,
						title:"新增排班",
						width : 700,
						height :220,
						top:($(window).height()-220)*0.5,   
			            left:($(window).width()-700)*0.5,
						modal : true,
						onload:function(){
						}
					});
				}
			},{
				id : 'btnedit',
				text : '编辑',
				iconCls : 'icon-edit',
				handler : function() {
					$('#btnsave').linkbutton('enable');
					if(selectedIds.indexOf(',')!=-1){
						editSingle(selectedIds);
					}else{
						$.messager.alert('提示','没有选择任何数据!');
					}
				}
			},{
				id : 'btncancel',
				text : '取消排班',
				iconCls : 'icon-cancel',
				handler : function() {
					$('#btnsave').linkbutton('enable');
					if(selectedIds.indexOf(',')!=-1){
						cancelSingle(selectedIds,status);
					}else{
						$.messager.alert('提示','没有选择任何数据!');
					}
				}
			},
			/* {
				id : 'btncut',
				text : '删除	',
				iconCls : 'icon-cut',
				handler : function() {
					$('#btnsave').linkbutton('enable');
					if(selectedIds.indexOf(',')!=-1){
					   deleteSingle(selectedIds);
					}else{
						$.messager.alert('提示','没有选择任何数据!');
					}
				}
			}, */{
				id : 'btnsearch',
				text : '条件查询',
				iconCls : 'icon-search',
				handler : function() {
					$("#searchForm").form('clear');
					$('#wndSearch').window({
						title:"查询",
						closed:false,
						width : 950,
						height : 200,
						top:($(window).height()-200)*0.5,   
			            left:($(window).width()-950)*0.5,
						modal : true
					});
				}
			}]
		});
		
		
		var p = $('#dataTable').datagrid('getPager');
		$(p).pagination({
			onBeforeRefresh : function() {
				//alert('before refresh');
			}		
		});
		
		//查询
		$("#btSch").click(function(){
			var v_doctor_name = $("#sch_doctorId").val();
			var v_hospital_id = $("#sch_hospital_id").combobox('getValue');
			var v_section_name = $("#sch_section_id").val();
			var v_sc_date = $("#sch_sc_date").combobox('getValue');
			var v_sch_batch_time = $('select[name=sch_batch_time]').val();
			if(v_sch_batch_time == null || v_sch_batch_time == '') {
				v_sch_batch_time ="";
			}
			$('#dataTable').datagrid('reload',{
					s_doctor_name: v_doctor_name,
				    s_hospital_id: v_hospital_id,
				    s_section_name: v_section_name,
					s_sc_date: v_sc_date,
					s_batch: v_sch_batch_time
			});
		});
		
		$(function () { 
	        var options = { 
	            //required: "true", 
	            //missingMessage: "必填项", 
	            formatter: function (date) { 
	                var y = date.getFullYear(); 
	                var m = date.getMonth() + 1; 
	                var d = date.getDate(); 
	                return y + "-" + (m < 10 ? ("0" + m) : m) + "-" + (d < 10 ? ("0" + d) : d) ; 
	            } 
	        } 
	       $("#sch_sc_date").datebox(options);
      	   $("#e_sc_date").datebox(options);
	    }); 
		
		//清除
		$("#bt_clear").click(function(){
			$("#searchForm").form('clear');
		});
		
		//保存
		$("#bt_submit").click(function(){
			$("#addForm").form('submit',{
				url:'<%=contextPath%>/admin/scheduleAction.do?method=save',
				onSubmit:function(){
					return validateForm();
				},		
				success:function(data){
					var data = eval('(' + data + ')');  
					$.messager.alert('提示',data.msg);
					if(data.flag=='success'){
					$("#addForm").form('clear');
					$('#wndAdd').window('close');
					$('#dataTable').datagrid('reload');
					}
				}
			});
		});
		
		$("#bt_close").click(function(){
			$('#wndAdd').window('close');
		});
		
		//load 医院列表、科室列表、医生列表
		$("#hospital_id").combobox({
			url:'<%=contextPath %>/admin/doctorAction.do?method=initHospital',
			editable:false,
			valueField:'id',
			textField:'name',	
			onSelect:function(rec)
			{
				var url = '<%=contextPath %>/admin/doctorAction.do?method=initSecList&hospital_id='+rec.id;
				$('#section_id').combobox({
					url:url,
					valueField:'id',
					textField:'name',
					editable:false,
					onSelect:function(rec){
						var sectionId = rec.id;
						hospitalId = $("#hospital_id").combobox('getValue');
						if(null==hospitalId || ''==hospitalId)
						{
							$("#section_id").combobox('setValue','');
							alert("医院和科室不能为空.");
							return;
						}
						var childUrl = '<%=contextPath %>/admin/scheduleAction.do?method=getDoctorName&section_id='+sectionId+'&hospital_id='+hospitalId;
						$("#e_doctorId").combobox({
							url: childUrl,
							valueField:'id',
							textField:'name',
							onSelect:function(rec2){
								var doctorName = rec2.name;
								$("#name").val(doctorName);
							}
						});
					}
				});
			}
		});
	});
	
	
	//编辑
	function editSingle(id){
		$("#addForm").form('clear');
		$.ajax({
			url:"<%=contextPath%>/admin/scheduleAction.do?method=edit&id="+id+"&radom="+Math.random(),
			type:"post",
			success:function(data){
				var data = eval('('+data+')');
				$("#id").val(data.id);
				$("#hospital_id").combobox('select',data.hospitalId);
				$("#hospital_id").trigger('onSelect');
				$("#hd_section_id").val(data.sectionId);//隐藏科室编号
				//根据医院编号查询科室
				var sectionIdUrl = '<%=contextPath %>/admin/scheduleAction.do?method=getSectionName&hospital_id='+data.hospitalId;
				$("#section_id").combobox({
					url: sectionIdUrl,
					valueField:'id',
					textField:'name',
					onSelect:function(rec){
						var childName = rec.name;
						$("#name").val(childName);
					},onLoadSuccess:function(){
						var hd_section_id = $("#hd_section_id").val();
						if(hd_section_id!=''&&hd_section_id!='0'){
							$("#section_id").combobox('setValue',hd_section_id);
						}
					}
				});
				$("#hd_doctor_id").val(data.doctorId);//隐藏医生编号 
				var doctorIdUrl = '<%=contextPath %>/admin/scheduleAction.do?method=getDoctorName&section_id='+data.sectionId+'&hospital_id='+data.hospitalId;
				 
				$("#e_doctorId").combobox({
					url: doctorIdUrl,
					valueField:'id',
					textField:'name',
					onSelect:function(rec2){
						var doctorName = rec2.name;
						$("#name").val(doctorName);
					},onLoadSuccess:function(){
						var hd_doctor_id = $("#hd_doctor_id").val();
						if(hd_doctor_id!=''&& hd_doctor_id!='0'){
							$("#e_doctorId").combobox('setValue',hd_doctor_id);
						}
					}
				});				
			    $("#e_sc_date").val(data.scDateStr);
				$("#e_batch_time").val(data.batch);
			}
		});
		$('#wndAdd').window({
			title:"编辑",
			closed:false,
			width : 700,
			height :220,
			top:($(window).height()-220)*0.5,   
            left:($(window).width()-700)*0.5,
			onload:function(){
			
			},
			modal : true
		});
	}
	
	//删除
	function deleteSingle(id){
		$.messager.confirm('删除','确定删除选择的医生排班记录吗?',function(r){
			if(r){
				$.ajax({
					url:"<%=contextPath%>/admin/scheduleAction.do?method=delete&id="+id,
					type:"post",
					success:function(data){
					var data = eval('(' + data + ')');
					$.messager.alert('提示',data.msg);
					selectedIds = "";//多选时保存id  删除成功清空
					$('#dataTable').datagrid('reload');
					}
				});
			}
		});
	}
	
	//取消排班
	function cancelSingle(id,status){
		if ('1' == status) {
			$.messager.alert('提示', '请选择状态为有效的记录！', 'info'); 
			$('#dataTable').datagrid('reload');
			return false;
		}
		$.messager.confirm('取消排班','确定取消选择的医生排班记录吗?',function(r){
			if(r){
				$.ajax({
					url:"<%=contextPath%>/admin/scheduleAction.do?method=cancelShedule&id="+id,
					type:"post",
					success:function(data){
					var data = eval('(' + data + ')');
					$.messager.alert('提示',data.msg);
					selectedIds = "";
					$('#dataTable').datagrid('reload');
					}
				});
			}
		});		
	}
	
	
	//保存验证
	function validateForm(){
		var msg = "";
		//医院
		if($("#hospital_id").combobox('getValue')=='') {msg+="请选择所属医院!<br/>";}
		if($("#section_id").combobox('getValue')=='') {msg+="请选择科室!<br/>";}
		if($("#e_doctorId").combobox('getValue')=='') {msg+="请选择医生!<br/>";}
		if($("#e_sc_date").combobox('getValue')=='') {msg+="请选择排班日期!<br/>";}
		//时段
		var batch = $('select[name=batch]').val();
		if(batch=='' || batch == null) {msg+="请选择排班时段!<br/>";}
		if(msg!='') {
			$.messager.alert('错误',msg);
			return false;
		} else {
			return true;
		}
	}

</script>
<title>专家管理</title>
</head>
<body>

<div id="wndSearch" class="easyui-window">
<div class="easyui-panel admin_search" data-option="fit:true" style="margin:2px 2px">
	<form id="searchForm">
		<table>
			<tr>	
				<td class="textr w80">医院：</td>
				<td class="" >
					<select class="easyui-combobox" id="sch_hospital_id"  name="hospital_id" 
						data-options="valueField:'id',textField:'name',url:'<%=contextPath %>/admin/doctorAction.do?method=initHospital'" >
						<option value="" selected>所有医院</option>
					</select>
				</td>
				<td class="textr w80">科室：</td>
				<td style="" >
					<input type="text" id="sch_section_id" name="section_id" style="width:100px;"/>
				</td>
			</tr>
			<tr>	
				<td class="textr w80">医生：</td>
				<td class="">
						<input type="text" id="sch_doctorId" name="doctorId" style="width:100px;"/>
				</td>
				<td class="textr w80">排班日期：</td>
			    <td class=""><input id="sch_sc_date" name="sc_date" type="text" />	
			 </tr>
			<tr>	
			  <td class="textr w80">排班时段：</td>
			  <td class="">
			    <select id="sch_batch_time" name="sch_batch_time">
					 <option value ="1">上午</option>
					 <option value ="2">下午</option>
               </select>
              </td>
	          <td align="center">
					<a href="javascript:void(0);" id="btSch" class="easyui-linkbutton" data-options="iconCls:'icon-search'">查 询</a>&nbsp;
					<a href="javascript:void(0);" id="bt_clear" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">清除</a>
			  </td>
			</tr>
		</table>
		</form>
	
	</div>
	</div>
	
	<div class="easyui-panel" data-option="fit:true" style="overflow:auto;">
		<table id="dataTable" class="easyui-datagrid">
		</table>
	</div>
	<div id="wndAdd" class="easyui-window">
		<div class="admin_search mAtuo"
			style="width: 520px; background-color: #fff;">
			
			<form id="addForm" method="post">
			<input type="hidden" name="id" id="id"/> 
			<table>
				<tr>
					<td class="textr">所属医院：</td>
						<td class=""><select id="hospital_id"
							class="easyui-combobox" name="hospitalId">
						</select></td>
				</tr>
				<tr>			
					<td class="textr w80">科&nbsp;&nbsp;室：</td>
					<td class="w180">
								<input class="easyui-combobox" id="section_id" name="sectionId" 
									 style="width:150px;" 
									 data-options="editable:false,valueField:'id',textField:'name',style:{width:150}" />
								<input type="hidden" id="hd_section_id" name="hd_section_id" value="" />
					</td>
					<td class="textr w80">医生：</td>
					<td class="w180">
							<input id="e_doctorId" class="easyui-combobox" name="doctorId" 
									style="width:150px;" 
									data-options="editable:false,valueField:'id',textField:'name',style:{width:150}"/>
					       <input type="hidden" id="hd_doctor_id" name="hd_doctor_id" value="" />
					</td>								
				</tr>
				<tr>
					  <td class="textr ">排班日期：</td>
					  <td class="">
					  <input id="e_sc_date" name="scDate" type="text" />
					  </td>
					  <td class="textr w80">排班时段：</td>
					  <td class="">
					    <select id="e_batch_time" name="batch" >
						  <option value ="1" selected="selected">上午</option>
						  <option value ="2">下午</option>
		               </select>
		              </td>
				</tr>
			</table>
			</form>
			<br>
			<div style="text-align: center;">
			<a href="javascript:void(0);" id="bt_submit" class="easyui-linkbutton" data-options="iconCls:'icon-ok'">保存</a>&nbsp;&nbsp;
			<a href="javascript:void(0);" id="bt_close" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'">关闭</a>
			</div>
		</div>
	</div>
	
	<div id="wndDetail" class="easyui-window" >
	<iframe id="frmDetail" src="" width="100%" height="370px;"
			frameborder="0" scrolling="no" style="border: 0px none;" ></iframe>
			</div>
</body>
</html>